<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view class="Mall4j page-scan-pick-box">
    <view class="prod">
      <view class="prod-box">
        <view
          v-for="(prodItem, index) in orderInfo.orderItems"
          :key="index"
          class="prod-item"
        >
          <view class="prod-common">
            <view class="prod-img-image">
              <img-show :src="prodItem.pic" />
            </view>
            <view class="prod-info">
              <view class="prod-name-price">
                <view class="name">
                  {{ prodItem.prodName }}
                </view>
                <view class="price">
                  ￥<text class="price-big">
                    {{
                      parsePrice(prodItem.price)[0]
                    }}
                  </text>{{ '.' + (parsePrice(prodItem.price)[1] || '00') }}
                </view>
              </view>
              <view class="prod-sku">
                <view
                  v-if="prodItem.skuName"
                  class="sku-name"
                >
                  {{
                    prodItem.skuName
                  }}
                </view>
                <view class="total">
                  ×{{ prodItem.prodCount }}
                </view>
              </view>
              <view class="prod-stutus">
                <view
                  v-if="orderInfo.orderType !== 0"
                  class="prod-type-item"
                >
                  {{
                    ['', '团购', '秒杀'][orderInfo.orderType]
                  }}
                </view>
                <view
                  v-if="prodItem.returnMoneySts && prodItem.returnMoneySts > 0"
                  class="prod-stutus-item"
                >
                  {{ orderStatus[prodItem.returnMoneySts + ''] }}
                </view>
                <view
                  v-if="orderInfo.orderMold === 1"
                  class="prod-type-item"
                >
                  虚拟
                </view>

                <view
                  v-if="
                    util.isAuthorities('admin:orderRefund:update') &&
                      orderInfo.refundType === 2 &&
                      prodItem.refundId
                  "
                  class="refund-btn"
                  @tap="toRefund(prodItem.refundId)"
                >
                  退款信息
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="order-info">
      <view class="order-info-item">
        <text class="title">
          订单编号:
        </text>
        <text class="text">
          {{ orderInfo.orderNumber }}
        </text>
        <view
          class="copy"
          @tap="copy"
        >
          复制
        </view>
      </view>
      <view class="order-info-item">
        <text class="title">
          下单时间:
        </text>
        <text class="text">
          {{ orderInfo.createTime }}
        </text>
      </view>
      <view class="order-info-item">
        <text class="title">
          订单类型:
        </text>
        <text class="text">
          {{
            ['普通订单', '团购订单', '秒杀订单'][orderInfo.orderType]
          }}
        </text>
      </view>
      <view class="order-info-item">
        <text class="title">
          付款方式:
        </text>
        <text
          v-if="orderInfo.payType === null || orderInfo.status === 1"
          class="text"
        >
          待支付
        </text>
        <text
          v-else
          class="text"
        >
          {{
            [
              '积分支付',
              '微信小程序支付',
              '支付宝PC端支付',
              '微信扫码支付',
              '微信H5支付',
              '微信公众号支付',
              '支付宝H5支付',
              '支付宝APP支付',
              '微信APP支付',
              '用户余额支付',
              'PayPal支付'
            ][orderInfo.payType]
          }}
        </text>
      </view>
      <view
        v-if="orderInfo.payTime"
        class="order-info-item"
      >
        <text class="title">
          支付时间:
        </text>
        <text class="text">
          {{ orderInfo.payTime }}
        </text>
      </view>
      <view
        v-if="orderInfo.nickName"
        class="order-info-item"
      >
        <text class="title">
          买家昵称:
        </text>
        <text class="text">
          {{ orderInfo.nickName }}
        </text>
      </view>
      <view
        v-if="orderInfo.remarks"
        class="order-info-item"
      >
        <text class="title">
          买家备注:
        </text>
        <text class="text">
          {{ orderInfo.remarks }}
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          核销次数:
        </text>
        <text
          v-if="orderInfo.writeOffNum === -1"
          class="text"
        >
          多次核销
        </text>
        <text
          v-if="orderInfo.writeOffNum === 1"
          class="text"
        >
          单次核销
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          核销开始时间:
        </text>
        <text class="text">
          {{ orderInfo.writeOffStart }}
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          核销结束时间:
        </text>
        <text class="text">
          {{ orderInfo.writeOffEnd || '无' }}
        </text>
      </view>
    </view>
    <view
      v-if="virtualRemarkList.length > 0"
      class="detail"
    >
      <view class="module-title">
        全部留言
      </view>
      <view
        v-for="(item, index) in virtualRemarkList"
        :key="index"
        class="detail-item"
      >
        <view class="detail-item-title">
          {{ item.name }}:
        </view>
        <view class="detail-item-content">
          {{ item.value }}
        </view>
      </view>
    </view>
    <view
      v-if="
        util.isAuthorities('order:virtual:update') &&
          orderInfo.orderMold == 1 &&
          (orderInfo.status === 3 || orderInfo.status === 5) &&
          (!orderInfo.refundStatus || orderInfo.refundStatus > 2) &&
          orderInfo.writeOffCodes &&
          orderInfo.writeOffCodes.length > 0
      "
      class="pick-btn"
      @tap="pickUpGoods"
    >
      确定核销
    </view>
    <view
      v-if="
        util.isAuthorities('admin:station:orderItemsDelivery') &&
          orderInfo.orderMold == 0 && orderInfo.status == 2
      "
      class="pick-btn"
      @tap="pickUpGoods"
    >
      提货
    </view>
  </view>
</template>

<script setup>
import util from '@/utils/util'
// 订单编号
let pickCode = ''
// 订单信息
const orderInfo = ref({})
// 用户留言列表
const virtualRemarkList = ref([])

onLoad((options) => {
  const _pickCode = options.pickCode
  if (_pickCode) {
    pickCode = _pickCode
  }
  getPickGoodsInfo()
})
onShow(() => {
})

// 获取商品信息
const getPickGoodsInfo = () => {
  uni.showLoading()
  const param = {
    url: '/order/order/getOrderByCode',
    method: 'GET',
    data: {
      code: pickCode
    },
    hasCatch: true
  }
  http.request(param).then(({ data: res }) => {
    orderInfo.value = res
    const _virtualRemarkList = JSON.parse(res.virtualRemark) || []
    virtualRemarkList.value = _virtualRemarkList.filter(item => {
      return item.value
    })
    uni.hideLoading()
  }).catch((data) => {
    uni.hideLoading()
    uni.showModal({
      title: '提示',
      content: data.msg,
      showCancel: false,
      confirmText: '确定',
      success: (res) => {
        if (res.confirm) {
          uni.redirectTo({
            url: '/pages/index/index'
          })
        }
      }
    })
  })
}

// 确定核销 或 提货
const pickUpGoods = () => {
  uni.showLoading()
  // orderMold 1虚拟订单(核销)   0普通订单(提货)
  const orderMold = orderInfo.value.orderMold
  const param = {
    url: orderMold === 1 ? '/admin/station/orderWriteOffByOrderNumber' : '/admin/station/changeStatusAndOrderStation',
    method: 'PUT',
    data: {
      orderNumber: orderInfo.value.orderNumber
    },
    hasCatch: true
  }
  http.request(param).then(() => {
    uni.hideLoading()
    uni.showToast({
      title: '操作成功',
      icon: 'none'
    })
    // 提示信息结束后，更新订单信息
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/index/index'
      })
    }, 1500)
  }).catch((res) => {
    uni.hideLoading()
    uni.showToast({
      title: res.data.msg,
      icon: 'none'
    })
  })
}

const parsePrice = (price) => {
  if (price.toString().indexOf('.') > -1) {
    return price.toString().split('.')
  }
  return [price]
}
// 复制文本
const copy = () => {
  util.uniCopy({
    content: orderInfo.value.orderNumber,
    success: (res) => {
      uni.showToast({
        title: res,
        icon: 'none'
      })
    },
    error: (e) => {
      uni.showToast({
        title: e,
        icon: 'none',
        duration: 3000
      })
    }
  })
}

</script>

<style lang="scss" scoped>
@use './scan-pick.scss';
</style>
